<template>
  <div class="app-container">
    <div class="app-container-main">
      <div class="app-container-main-col flex-1">
        <filter-config-box
          :options="filterOptions"
          v-model="queryParams"
          @search="handleQuery"
          @onReset="resetQuery"
        />
        <el-card class="table-card">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd()"
                >新增</el-button
              >
            </el-col>
            <right-toolbar
              :showSearch.sync="showSearch"
              @queryTable="getList"
            ></right-toolbar>
          </el-row>

          <el-table ref="tables" v-loading="loading" :data="list">
            <el-table-column label="序号" type="index" align="center">
              <template slot-scope="scope">
                <span>{{
                  (queryParams.pageNum - 1) * queryParams.pageSize +
                  scope.$index +
                  1
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              label="账单当月总金额"
              :show-overflow-tooltip="true"
              align="center"
              prop="spendTotalPrice"
            >
              <template #default="{ row }">
                {{ "￥" + row.spendTotalPrice + "元" }}
              </template>
            </el-table-column>
            <el-table-column label="每月工资" align="center" prop="wages">
              <template #default="{ row }">
                {{ "￥" + row.wages + "元" }}
              </template>
            </el-table-column>
            <el-table-column label="每月存款" align="center" prop="deposit">
              <template #default="{ row }">
                <span
                  :style="{
                    color: row.deposit > 0 ? '#67c23a' : '#f56c6c',
                    marginRight: '5px',
                  }"
                  >{{ "￥" + row.deposit + "元" }}</span
                >
                <i
                  style="color: #f56c6c"
                  v-if="row.deposit < 0"
                  class="el-icon-bottom"
                ></i>
                <i style="color: #67c23a" v-else class="el-icon-top"></i>
              </template>
            </el-table-column>
            <el-table-column
              label="账单日期"
              align="center"
              prop="depositTime"
            />
            <el-table-column label="用户名称" align="center" prop="createBy" />
            <el-table-column label="备注" align="center" prop="remark" />
            <el-table-column
              label="创建时间"
              align="center"
              prop="createTime"
            />
            <el-table-column
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handlePreview(scope.row)"
                  >详情</el-button
                >
                <el-button
                  size="mini"
                  type="text"
                  icon="el-icon-edit"
                  @click="handleUpdate(scope.row)"
                  >修改</el-button
                >
              </template>
            </el-table-column>
            <div slot="empty" class="emptyBg">
              <el-empty :image-size="200"></el-empty>
            </div>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </div>
    </div>
    <!-- 添加和修改 -->
    <addTotalBill ref="addTotalBill" :success="success" />
  </div>
</template>

<script>
import { totalBillDeatil, totalBillList } from "@/api/bill/billList.js";
import addTotalBill from "./addTotalBill.vue";

export default {
  name: "BillList",
  components: { addTotalBill },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      list: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        // 日期范围
        depositTime: undefined,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handlePreview(row) {
      this.$router.push({
        name: "BillList",
        params: { depositTime: row.depositTime },
      });
    },
    /** 成功后的回调 */
    success() {
      this.getList();
    },
    /** 添加 */
    handleAdd() {
      this.$refs.addTotalBill.open("add", {});
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      totalBillDeatil({ id: row.id }).then((response) => {
        this.$refs.addTotalBill.open("update", response.data);
      });
    },
    /** 查询登录日志列表 */
    getList() {
      this.loading = true;
      totalBillList(this.queryParams).then((response) => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.queryParams.pageNum = 1;
      this.getList();
    },
  },
  computed: {
    getRoles() {
      return this.$store.state.user.roles;
    },
    filterOptions() {
      return [
        {
          tag: "date",
          prop: "depositTime",
          label: "账单日期",
          labelWidth: "68px",
          attrs: {
            style: {
              width: "240px",
            },
            type: "month",
            format: "yyyy-MM",
            "value-format": "yyyy-MM", // HH:mm:ss
          },
        },
      ];
    },
  },
};
</script>

